package top.aigoo.project01.controller.local;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import top.aigoo.project01.entity.LocalAuth;
import top.aigoo.project01.entity.PersonInfo;
import top.aigoo.project01.entity.dto.LocalAuthExecution;
import top.aigoo.project01.enums.LocalAuthStateEnum;
import top.aigoo.project01.service.LocalAuthService;
import top.aigoo.project01.utils.CodeUtil;
import top.aigoo.project01.utils.HttpServletRequestUtil;
import top.aigoo.project01.utils.MD5;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 请填写该类完成的功能
 * @Author: Mr.OYSHUO.欧阳大叔  mail:1642302522@qq.com
 * @Version 1.0.0
 * @Date: 2021/9/12 23:53
 */

@Controller
@RequestMapping(value = "local", method = {RequestMethod.GET, RequestMethod.POST})
public class LocalAuthController {

    @Autowired
    private LocalAuthService localAuthService;


    /**
     * 将用微信户信息与平台账号绑定
     */
    @RequestMapping(value = "/bindlocalauth", method = RequestMethod.POST)
    @ResponseBody
    private Map<String, Object> bindLocalAuth(HttpServletRequest request) {

        Map<String, Object> modelMap = new HashMap<String, Object>();
        //验证码校验
        if (!CodeUtil.checkVerifyCode(request)) {
            modelMap.put("success", false);
            modelMap.put("errMsg", "输入了错误的验证码");
            return modelMap;
        }
        //获取输入的账号
        String userName = HttpServletRequestUtil.getString(request, "userName");
        //获取输入的密码
        String password = HttpServletRequestUtil.getString(request, "password");
        //从session中获取当前用户的信息(用户一旦通过微信登录后,便能够获取到用户信息)
        PersonInfo user = (PersonInfo) request.getSession().getAttribute("user");
        //非空判断，要求账号密码以及当前的用户session非空
        if (userName != null && password != null && user != null
                && user.getUserId() != null) {
            password = MD5.getMd5(password);
            //创建LocalAuth对象并赋值
            LocalAuth localAuth = new LocalAuth();
            localAuth.setUserName(userName);
            localAuth.setPassword(password);
            //localAuth.setUserId(user.getUserId());
            localAuth.setPersonInfo(user);
            //绑定账户
            LocalAuthExecution le = localAuthService.bindLocalAuth(localAuth);
            if (le.getState() == LocalAuthStateEnum.SUCCESS.getState()) {
                modelMap.put("success", true);
            } else {
                modelMap.put("success", false);
                modelMap.put("errMsg", le.getStateInfo());
            }
        } else {
            modelMap.put("success", false);
            modelMap.put("errMsg", "用户名和密码均不能为空");
        }
        return modelMap;
    }

    /**
     * 修改密码
     */
    @RequestMapping(value = "/changelocalpwd", method = RequestMethod.POST)
    @ResponseBody
    private Map<String, Object> changeLocalPwd(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        //验证码校验
        if (!CodeUtil.checkVerifyCode(request)) {
            modelMap.put("success", false);
            modelMap.put("errMsg", "输入了错误的验证码");
            return modelMap;
        }
        //获取账号
        String userName = HttpServletRequestUtil.getString(request, "userName");
        //获取原密码
        String password = HttpServletRequestUtil.getString(request, "password");
        //获取新密码
        String newPassword = HttpServletRequestUtil.getString(request, "newPassword");
        //从session中获取当前用户信息(用户一旦通过微信登录之后，便能够获取到用户的信息)
        PersonInfo user = (PersonInfo) request.getSession().getAttribute("user");

        long employeeId = 0;
        if (user != null && user.getUserId() != null) {
            employeeId = user.getUserId();
        } else {
            employeeId = 1;
        }
        //非空判断，要求账号新、旧密码以及当前用户session非空，且新、旧密码不相同
        if (userName != null && password != null && newPassword != null
                && user != null && user.getUserId() != null && !password.equals(newPassword)) {
            try {
                // 查看原先账户，看看与输入的账号是否一致，不一致则认为是非法操作
                LocalAuth localAuth = localAuthService.getLocalAuthByUserId(user.getUserId());
                if (localAuth == null || !localAuth.getUserName().equals(userName)) {
                    //不一致则直接退出
                    modelMap.put("success", false);
                    modelMap.put("errMsg", "输入的账号非本地登录的账号");
                    return modelMap;
                }
                //修改平台账户的用户密码
                LocalAuthExecution le = localAuthService.modifyLocalAuth(
                        user.getUserId(), userName, password, newPassword);
                if (le.getState() == LocalAuthStateEnum.SUCCESS.getState()) {
                    modelMap.put("success", true);
                } else {
                    modelMap.put("success", false);
                    modelMap.put("errMsg", le.getStateInfo());
                }
            } catch (RuntimeException e) {
                modelMap.put("success", false);
                modelMap.put("errMsg", e.toString());
                return modelMap;
            }

        } else {
            modelMap.put("success", false);
            modelMap.put("errMsg", "请输入密码");
        }
        return modelMap;
    }

    /**
     * 登录验证
     */
    @RequestMapping(value = "/logincheck", method = RequestMethod.POST)
    @ResponseBody
    private Map<String, Object> ownerLoginCheck(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        //进行验证码校验的标识符
        boolean needVerify = HttpServletRequestUtil.getBoolean(request, "needVerify");
        if (needVerify && !CodeUtil.checkVerifyCode(request)) {
            modelMap.put("success", false);
            modelMap.put("errMsg", "输入了错误的验证码");
            return modelMap;
        }
        //获取输入账号
        String userName = HttpServletRequestUtil.getString(request, "userName");
        //获取输入密码
        String password = HttpServletRequestUtil.getString(request, "password");
        //非空校验
        if (userName != null && password != null) {

            password = MD5.getMd5(password);
            //传入账号和密码去获取平台账号信息
            LocalAuth localAuth = localAuthService.getLocalAuthByUsernameAndPwd(userName, password);
            if (localAuth != null) {
                //如果取到账号信息则登录成功
                //if (localAuth.getPersonInfo().getShopOwnerFlag() == 1) {
                //    modelMap.put("success", true);
                //    request.getSession().setAttribute("user", localAuth.getPersonInfo());
                //} else {
                //    modelMap.put("success", false);
                //    modelMap.put("errMsg", "非管理员没有权限访问");
                //}
                modelMap.put("success", true);
                //同时在session里面设置用户信息
                request.getSession().setAttribute("user", localAuth.getPersonInfo());
            } else {
                modelMap.put("success", false);
                modelMap.put("errMsg", "用户名或密码错误");
            }
        } else {
            modelMap.put("success", false);
            modelMap.put("errMsg", "用户名和密码均不能为空");
        }
        return modelMap;
    }

    /**
     * 退出登录,当用户点击登出按钮的时候注销session
     *
     */
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    @ResponseBody
    private Map<String, Object> logout(HttpServletRequest request,
                                       HttpServletResponse response) throws IOException {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        //将用户session设置为空
        request.getSession().setAttribute("user", null);
        modelMap.put("success", true);
        return modelMap;
    }
}
